<?php 

function list_send_timegaji(){

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%;text-align:center'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:10%;text-align:center'),   
		'Nama Karyawan'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'Email'=>array('style'=>'border-bottom:2px solid;width:20%'),   
	);
	$end=get_batas_tmt() ;
	if(isset($_GET['key']))
	$query = "
			SELECT a.karyawan_id FROM karyawan a 
			INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
			INNER JOIN biodata c ON a.biodata_id = c.biodata_id 
			WHERE b.tmb  <= '{$end}' 
			AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			AND ( c.nama LIKE '%{$_GET['key']}%' OR a.nik ='{$_GET['key']}' )
		";
	else
	$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 	
			";	
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	
	while($ey = my_fetch_array($result )){
		$karyawan = loaddata_karyawan($ey['karyawan_id']); 
		$jabatan =  loaddata_jabatan($karyawan['jabatan_id']); 
		$i++;
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=> $karyawan['nama_gelar'],        
			'jabatan'=> $jabatan['nama_jabatan'],        
			'email'=> $karyawan['email_km'] 
		);
		
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';
	$menuontop = array(  
		'Broadcasting' => array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=broadcast\' });'),
		//'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&key='.$_GET['key'].'\';'),
	);	
	
	$printmenu = additional_menu_on_list( $menuontop, $search);
	return $printmenu .table_builder($header , $datas ,  15 ,false , $paging  );
}


function page_proses_range(){

	$view = form_header( "dat" , "dat"  );
	 
	$view .= '<input type="hidden"  value="'.$_GET['com'].'" name="com" />';  
	$view .= '<input type="hidden"  value="broadcast_page_proses" name="task" />'; 
	$view .= '<input type="hidden"  value="'.(int)$_GET['periode_id'].'" name="periode_id" />'; 
	$form_start = '<input type="text" size="7" value="'.$nik_start.'" name="nik_start" />';
	$form_end = '<input type="text" size="7" value="'.$nik_end.'" name="nik_end" />';
	$view .= form_field_display( $form_start.' s/d '.$form_end  , "NIK Range"   );
	 
	$excel = array(
		'value' => (  ' Broadcast gaji '  ),
		'onclick' => 'location.href=\'index.php?com='.$_GET['com'].'&task=broadcast_page_proses&periode_id=\'+document.dat.periode_id.value+\'&nik_start=\'+document.dat.nik_start.value+\'&nik_end=\'+document.dat.nik_end.value', 
		'type'=>'button',
		'style'=>'width:120px',
		'class'=>'main_button'
	);
	$form_excel	= form_dynamic($excel);
 
	$view .= form_field_display(   $form_cetak .' '.$form_excel, "&nbsp;" ,  "" );
	$view .= form_footer( );
	$view = str_replace("method=\"post\"","method=\"get\"",$view);
	return $view;
}

function page_proses(){ 
	my_set_code_js('
	$(document).ready(function() {
       	$(\'#onebar\').load(\'index.php?com='.$_GET['com'].'&task=sendproses&nik_start='.$_GET['nik_start'].'&nik_end='.$_GET['nik_end'].'&periode_id='.$_GET['periode_id'].'&'.rand(0,9).'='.rand(0,99999).'\'); 
	});
	');
	return '<div id="onebar" style="width:100%;text-align:center;"><img src="components/system/jquery/facebox/loading.gif" /><br/>On processing!!!</div>';
}

function sendproses(){
	 
	$periode_id=get_periode_gaji() - 1 ;
	$end=get_batas_tmt() ;
	$query = " SELECT a.karyawan_id FROM karyawan a
				INNER JOIN kekaryawanan b ON b.karyawan_id = a.karyawan_id 
				WHERE b.tmb  <= '{$end}' 
				AND a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
				AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE tmt <= '{$end}') 
				AND ( a.nik BETWEEN '{$_GET['nik_start']}' AND '{$_GET['nik_end']}' )	
			";
 	$result = my_query($query);
	while( $row = my_fetch_array($result) ){
		 
		send_slip_karyawan($row['karyawan_id'] , $periode_id);
	}
	return 'Done ';
}

function send_slip_karyawan($karyawan_id , $periode_id){
	$allow = array('1000010','1000290','0000116','1000028','1000116');
	$check_target = "
	SELECT nama, email_km ,b.nik FROM biodata a 
		INNER JOIN karyawan b ON a.biodata_id = b.biodata_id
		INNER JOIN kekaryawanan c ON c.karyawan_id = b.karyawan_id
		WHERE b.karyawan_id = {$karyawan_id} AND email_km <> '' LIMIT 1";
	$result = my_query($check_target); 
	
	if( $row = my_fetch_array($result) ){  
		//if(! in_array( $row['nik'] , $allow ) )continue;

		$pathslipgaji = SLIP_GAJI_PATH.  $periode_id .'_'. $row['nik'] .'.pdf' ;
		$valid_email = valid_email_address( $row['email_km'] );
		$feedbackdate = date('d-M-Y', ( time() + 5 * 24* 60 * 60) );
		if( $valid_email ){
			$t = fopen("http://hr-kmh.com/index.php?com=report&task=slip_gaji_save&periode_id={$periode_id}&karyawan_id={$karyawan_id}&periode_id={$periode_id}" , "r");

			if(! file_exists($pathslipgaji) ){ 
				continue;
			}
		   	 
			$send_status =  send_email_and_attachment($row['nama'],$row['email_km'], $pathslipgaji , $periode_id , $feedbackdate);
  
			//if( $row['nik'] =='1000290') {
			//	send_email_and_attachment($row['nama'],'gunturris@gmail.com', $pathslipgaji , $periode_id);
			//}
 
			//sent_email_status($row['nik'], $row['email_km'] , "http://hr-kmh.com/index.php?com=report&task=slip_gaji_save&periode_id={$periode_id}&karyawan_id={$karyawan_id}&periode_id={$periode_id}");
			return $send_status;
		}
		return true;
	}
	
	return false;
}


function send_email_and_attachment($to,$email, $pathattachment , $periode_id , $feedbackdate){
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$nama_periode = date( 'm-Y' , strtotime( $periode['tanggal']) ) ;
	$mail             = new PHPMailer(); 
	  
	$mail->IsSMTP(); // telling the class to use SMTP 
	$mail->SMTPDebug  = 0;                      
	$mail->SMTPSecure = 'tls';                 
	$mail->Host       = "localhost"; 
	$mail->Port       = 25; 
	$mail->Username = "admin"; // SMTP username 
	$mail->Password = "dordordor"; // SMTP password   

	$mail->From = 'admin@hr-kmh.com';
	$mail->FromName = 'SDM PT. KRAKATAU MEDIKA';
	$mail->AddReplyTo("admin@hr-kmh.com","No-Reply");

	$mail->AddAddress( $email , $to ); 
	
	if( $email == '1000290@hr-kmh.com' ){
		$mail->AddBCC('gunturris@gmail.com', 'Guntur');
	}
	$mail->Subject   = "Slip gaji ".$nama_periode;
	$mail->Body     = "
Yth Bpk/ Ibu/ Sdr/i {$to} 
Berikut terlampir slip gaji periode ".$nama_periode ."

NB:   Apabila data pada slip gaji ada yang tidak sesuai,
      dapat menghubungi/ datang langsung ke SDM
      untuk dikoreksi paling lambat tanggal {$feedbackdate} 
";
	$mail->AddAttachment($pathattachment);
	return $mail->Send();
}

 function get_gaji($karyawan_id , $kalkulasi_periode){ 
		$query = " SELECT * FROM pr_gaji 
			WHERE periode_id={$kalkulasi_periode}
			AND karyawan_id={$karyawan_id}"
		;
		$result = my_query($query);	
		$datas = my_fetch_array($result);	
		return $datas;
	}
	
	function get_data_list_rapel($komponen_id ,$karyawan_id, $periode_id){
		$query = " SELECT nominal FROM pr_rapel_komponen_karyawan a
			INNER JOIN pr_rapel_periode b ON a.rapel_id = b.rapel_id
			INNER JOIN pr_periode c ON b.periode_bayar = c.tanggal
			WHERE c.periode_id = {$periode_id} AND a.komponen_id = {$komponen_id} AND karyawan_id = {$karyawan_id}";
		$result = my_query($query);
		if(my_num_rows($result) > 0){
			$row = my_fetch_array($result);
			return $row['nominal'];
		}
		return 0;
	}
	
	function get_data_list($karyawan_id , $kalkulasi_periode, $jenis){

		$query = "SELECT a.komponen_id,a.kalkulasi  
				FROM pr_gaji_komponen a 
				LEFT JOIN pr_komponen_gaji b ON a.komponen_id=b.komponen_gaji_id
				WHERE a.periode_id={$kalkulasi_periode} 
				AND a.karyawan_id={$karyawan_id} 
				AND b.tipe={$jenis} 
				AND a.kalkulasi<>0 
				ORDER BY a.komponen_id" ;

		$rs = my_query($query);
		$datas = array();
		while($row = my_fetch_array($rs)){
			$datas[$row['komponen_id']] = $row['kalkulasi'];
		}
		return $datas;
	}

	function ottime($karyawan_id, $periode_id){

		$query = "SELECT SUM(hourpay) AS fdata
			FROM tmn_overtime 
			WHERE karyawan_id ={$karyawan_id} AND out_of_count = 'N'
			AND periode_id={$periode_id}
			LIMIT 1
		";
		
		$res = my_query($query);
		$row = my_fetch_array($res);
		 
		return $row['fdata'];
	}

function sent_email_status($nik , $email , $url){
	$datas = array(
		'nik'=>my_type_data_str($nik ),
		'email'=>my_type_data_str($email ),
		'url_link'=>my_type_data_str($url),
		'posted_time'=>my_type_data_function('NOW()') 
	);
	return my_insert_record('sent_email_slip_gaji' , $datas );
}